package com.vgooo.client.vchannel.news;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.vgooo.bean.NewListInfo;
import com.vgooo.client.R;
import com.vgooo.util.BitmapUtils;
import com.vgooo.util.DebugTools;

public class NewsAdapter extends BaseAdapter {

	private final static String className = "NewsAdapter";
	private List<NewListInfo> newsInfos;// 新闻消息
	private LayoutInflater inflater;// 新闻视图加载器
	public Context context;

	public NewsAdapter(List<NewListInfo> videoInfos, Context context) {
		// TODO Auto-generated constructor stub
		setVideoInfos(videoInfos);
		this.context = context;
		inflater = LayoutInflater.from(context);
	}

	public List<NewListInfo> getNews() {
		return newsInfos;
	}

	public void setVideoInfos(List<NewListInfo> newsInfos) {
		DebugTools.log(className, "videoInfos = " + newsInfos);
		// log("videoInfos = " + newsInfos);
		if (newsInfos != null) {
			this.newsInfos = newsInfos;
		} else {
			this.newsInfos = new ArrayList<NewListInfo>();
		}
	}

	/**
	 * 增加数据
	 * 
	 * @param videos
	 */
	public void chanageData(List<NewListInfo> videos) {
		if (videos != null && !videos.isEmpty()) {
			this.newsInfos.clear();
			this.newsInfos = videos;
			notifyDataSetChanged();
		}
	}

	/**
	 * 增加数据
	 * 
	 * @param news
	 */
	public void adddata(List<NewListInfo> news) {
		if (news != null && !news.isEmpty()) {
			this.newsInfos.addAll(news);
			// log("news = " + news);
			DebugTools.log(className, "news = " + news);
			notifyDataSetChanged();
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return newsInfos.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return newsInfos.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public int getItemViewType(int position) {
		NewListInfo newsInfo = newsInfos.get(position);

		if (NewListInfo.NEWS_TYPE_ALBUM.equals(newsInfo.newsType)) {
			return 1;
		} else
//			if (NewListInfo.NEWS_TYPE_NEWS.equals(newsInfo.newsType))
			{
			return 0;
		} 
//			else {
//			return 2;
//		}
	};

	public int getViewTypeCount() {
		return 2;
	};

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		NewListInfo newsInfo = newsInfos.get(position);
		ViewHolder holder = null;

		if (convertView == null) {
			holder = new ViewHolder();
			if (NewListInfo.NEWS_TYPE_ALBUM.equals(newsInfo.newsType)) {

				convertView = inflater.inflate(R.layout.news_album_item, null);
				// TYPE_ALBUM
				holder.albumTitle = (TextView) convertView
						.findViewById(R.id.news_item_album_tile);
				holder.pic1 = (ImageView) convertView
						.findViewById(R.id.news_item_album_image1);
				holder.pic2 = (ImageView) convertView
						.findViewById(R.id.news_item_album_image2);
				holder.pic3 = (ImageView) convertView
						.findViewById(R.id.news_item_album_image3);
				holder.albumPing = (TextView) convertView
						.findViewById(R.id.news_item_album_review);
//			} else if (NewListInfo.NEWS_TYPE_NEWS.equals(newsInfo.newsType)){
//				convertView = inflater.inflate(R.layout.news_item, null);
//				// TYPE_NEWS
//				holder.newsImage = (ImageView) convertView
//						.findViewById(R.id.news_item_video_small_image);
//				holder.newsTitle = (TextView) convertView
//						.findViewById(R.id.news_item_video_title);
//				holder.newsDesc = (TextView) convertView
//						.findViewById(R.id.news_item_video_body);
//				holder.newsPing = (TextView) convertView
//						.findViewById(R.id.news_item_video_review);
			}else{
				convertView = inflater.inflate(R.layout.news_video_item, null);
				// TYPE_VIDEO
				holder.newsImage = (ImageView) convertView
						.findViewById(R.id.news_item_video_small_image);
				holder.newsVideo = (ImageView) convertView.findViewById(R.id.news_item_video_image);
				holder.newsTitle = (TextView) convertView
						.findViewById(R.id.news_item_video_title);
				holder.newsDesc = (TextView) convertView
						.findViewById(R.id.news_item_video_body);
				holder.newsPing = (TextView) convertView
						.findViewById(R.id.news_item_video_review);
			}
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		if (NewListInfo.NEWS_TYPE_ALBUM.equals(newsInfo.newsType)) {

			holder.albumTitle.setText(newsInfo.news_title);
			holder.albumPing.setText(newsInfo.news_ping);

			if (Integer.parseInt(newsInfo.news_ping) > 19) {
				holder.albumPing.setBackgroundResource(R.drawable.ping_bg_red);
			} else {
				holder.albumPing.setBackgroundResource(R.drawable.ping_bg_gray);
			}

			BitmapUtils.loaderImage(newsInfo.picture.get(0), holder.pic1,
					context);
			BitmapUtils.loaderImage(newsInfo.picture.get(1), holder.pic2,
					context);
			BitmapUtils.loaderImage(newsInfo.picture.get(2), holder.pic3,
					context);
		} else {

			if (NewListInfo.NEWS_TYPE_NEWS.equals(newsInfo.newsType)){
				holder.newsVideo.setVisibility(View.INVISIBLE);
			}else{
				holder.newsVideo.setVisibility(View.VISIBLE);
			}
			holder.newsTitle.setText(newsInfo.news_title);
			String newsDesc = newsInfo.news_abstract;
			if (newsDesc.length() > 30) {
				newsDesc = newsDesc.substring(0, 30) + "...";
			}
			holder.newsDesc.setText(newsDesc);
			if (Integer.parseInt(newsInfo.news_ping) > 9) {
				holder.newsPing.setBackgroundResource(R.drawable.ping_bg_red);
			} else {
				holder.newsPing.setBackgroundResource(R.drawable.ping_bg_gray);
			}
			holder.newsPing.setText(newsInfo.news_ping);

			BitmapUtils.loaderImage(newsInfo.news_thumbnail, holder.newsImage,
					context);
		}
		return convertView;
	}

	private class ViewHolder {

		LinearLayout newsLayout;
		LinearLayout albumLayout;

		ImageView newsImage;
		ImageView newsVideo;
		TextView newsTitle;
		TextView newsDesc;
		TextView newsPing;

		TextView albumTitle;
		TextView albumPing;
		ImageView pic1;
		ImageView pic2;
		ImageView pic3;
	}

	/**
	 * 打印调试信息
	 * 
	 * @param info
	 */
	private void log(String info) {
		Log.i(this.getClass().getSimpleName(), info);
	}

}
